﻿package mortal.game.view.vip
{
    import Message.Public.*;
    import flash.events.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.proxy.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.systemSetting.*;
    import mortal.game.view.vip.Wing288.*;
    import mortal.game.view.vip.panel.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class VipController extends Controller
    {
        private var _vipModule:VipModule;
        private var _vipCache:VipCache;
        private var _vipProxy:PlayerProxy;

        public function VipController()
        {
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this._vipCache = Cache.instance.vip;
            this._vipProxy = GameProxy.player;
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._vipModule == null)
            {
                this._vipModule = new VipModule();
                this._vipModule.addEventListener(WindowEvent.SHOW, this.onWinShow);
                this._vipModule.addEventListener(WindowEvent.CLOSE, this.onWinClose);
            }
            return this._vipModule;
        }// end function

        override public function get view() : IView
        {
            if (_view == null)
            {
                _view = this.initView();
            }
            return _view;
        }// end function

        override protected function initServer() : void
        {
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.checkIsShowWing288Icon);
            NetDispatcher.addCmdListener(ServerCommand.VipLevelUp, this.vipLevelUp);
            NetDispatcher.addCmdListener(ServerCommand.VipFreeInfoUpdate, this.vipFreeInfoHandler);
            NetDispatcher.addCmdListener(ServerCommand.NewDayUpdate, this.newDayUpdate);
            NetDispatcher.addCmdListener(ServerCommand.FirstRechargeWingGet, this.checkIsShowWing288Icon);
            NetDispatcher.addCmdListener(ServerCommand.SystemClientSettingGot, this.checkIsShowWing288Icon);
            Dispatcher.addEventListener(EventName.VipWinOpen, this.openWin);
            Dispatcher.addEventListener(EventName.VIP_ShowFirstRechageWin, this.showVipFirstRechageWinHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            Dispatcher.addEventListener(EventName.Wing288IconShowHide, this.wing288IconShowHide);
            return;
        }// end function

        private function onWinShow(event:Event) : void
        {
            NetDispatcher.addCmdListener(ServerCommand.ReChargeMoney, this.rechargeUpdate);
            NetDispatcher.addCmdListener(ServerCommand.VipRewardGet, this.updateRewardBtn);
            NetDispatcher.addCmdListener(ServerCommand.VipFreeInfoUpdate, this.updateDailyBtn);
            Dispatcher.addEventListener(EventName.VipGetReward, this.getVipReward);
            Dispatcher.addEventListener(EventName.VipWinChangeTab, this.changeTab);
            return;
        }// end function

        private function onWinClose(event:Event) : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.ReChargeMoney, this.rechargeUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.VipRewardGet, this.updateRewardBtn);
            NetDispatcher.removeCmdListener(ServerCommand.VipFreeInfoUpdate, this.updateDailyBtn);
            Dispatcher.removeEventListener(EventName.VipGetReward, this.getVipReward);
            Dispatcher.removeEventListener(EventName.VipWinChangeTab, this.changeTab);
            return;
        }// end function

        private function wing288IconShowHide(event:DataEvent) : void
        {
            var _loc_2:* = event.data;
            if (_loc_2)
            {
                this.checkIsShowWing288Icon();
            }
            else if (Wing288Icon.isViewShow)
            {
                Wing288Icon.instance.hide();
            }
            return;
        }// end function

        private function checkIsShowWing288Icon(param1:Object = null) : void
        {
            var _loc_4:Date = null;
            var _loc_5:Date = null;
            var _loc_6:int = 0;
            var _loc_2:* = Cache.instance.role.entityInfo.level;
            if (_loc_2 < 49)
            {
                return;
            }
            var _loc_3:* = ItemConfig.instance.getConfig(GameConst.TConstExperWingShapeCardCode);
            if (Cache.instance.vip.isCanGetWin)
            {
                Wing288Icon.instance.show();
                Wing288Icon.instance.updateState(1);
            }
            else if (_loc_2 < _loc_3.effect)
            {
                Wing288Icon.instance.show();
                Wing288Icon.instance.updateState(0);
            }
            else if (_loc_2 >= _loc_3.effect)
            {
                if (Cache.instance.vip.wing288GetTime)
                {
                    _loc_4 = ClockManager.instance.nowDate;
                    _loc_5 = Cache.instance.vip.wing288GetTime;
                    _loc_6 = int(_loc_3.existTime) * 60 * 1000 + _loc_5.time - _loc_4.time;
                    if (_loc_6 <= 0)
                    {
                        if (Wing288Icon.isViewShow)
                        {
                            Wing288Icon.instance.hide();
                        }
                    }
                    else
                    {
                        Wing288Icon.instance.show();
                        Wing288Icon.instance.updateState(2, _loc_6 * 0.001);
                    }
                }
                else if (!ClientSetting.local.getIsDone(IsDoneType.IsClickWing288))
                {
                    Wing288Icon.instance.show();
                    Wing288Icon.instance.updateState(3);
                }
                else
                {
                    if (Wing288Icon.isViewShow)
                    {
                        Wing288Icon.instance.hide();
                    }
                    NetDispatcher.removeCmdListener(ServerCommand.FirstRechargeWingGet, this.checkIsShowWing288Icon);
                    NetDispatcher.removeCmdListener(ServerCommand.SystemClientSettingGot, this.checkIsShowWing288Icon);
                    Cache.instance.role.roleEntityInfo.removeEventListener(EntityInfoEventName.UpdateLevel, this.checkIsShowWing288Icon);
                    Dispatcher.removeEventListener(EventName.Wing288IconShowHide, this.wing288IconShowHide);
                }
            }
            return;
        }// end function

        private function sceneUpdateHandler(event:DataEvent) : void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            this.vipLevelUp(null);
            return;
        }// end function

        private function showVipFirstRechageWinHandler(event:DataEvent) : void
        {
            VipFirstRechargeIcon.instance.show();
            return;
        }// end function

        private function openWin(event:DataEvent) : void
        {
            GameManager.instance.popupModuleType(ModuleType.Vip);
            return;
        }// end function

        private function vipFreeInfoHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as SVipInfo;
            if (_loc_2.expiredDt.time <= ClockManager.instance.nowDate.time)
            {
                this.openVipExperienceTimeOverWin();
            }
            return;
        }// end function

        private function vipLevelUp(param1:Object) : void
        {
            if (Cache.instance.vip.level < 10)
            {
                if (!VipIcon.isViewShow)
                {
                    VipIcon.instance.show();
                }
            }
            else if (VipIcon.isViewShow)
            {
                VipIcon.instance.hide();
            }
            if (GameController.vip.isViewShow)
            {
                this._vipModule.vipLevelUp();
            }
            if (Cache.instance.vip.level > 0)
            {
                NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsAdd, this.openVipExperienceWin);
            }
            else
            {
                NetDispatcher.addCmdListener(ServerCommand.BackPackItemsAdd, this.openVipExperienceWin);
            }
            if (!GameController.autoGuide.isGuidingVip())
            {
                return;
            }
            return;
        }// end function

        private function rechargeUpdate(param1:Object) : void
        {
            if (GameController.vip.isViewShow)
            {
                this._vipModule.rechargeUpdate();
            }
            return;
        }// end function

        private function getVipReward(event:DataEvent) : void
        {
            this._vipProxy.getVipReWard(event.data.level, event.data.type, event.data.fun);
            return;
        }// end function

        private function updateRewardBtn(param1:Object) : void
        {
            this._vipModule.updateRewardBtn();
            return;
        }// end function

        private function flyToBackPack(param1:Object) : void
        {
            this._vipModule.flyToBackPack();
            return;
        }// end function

        private function openVipExperienceWin(param1:Object) : void
        {
            var _loc_2:* = param1[0] as ItemData;
            if (ItemsUtil.isVipExperienceCard(_loc_2))
            {
                VipExperienceWin.instance.show();
                VipExperienceWin.instance.itemData = _loc_2;
                VipExperienceWin.instance.updateState(false);
                NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsAdd, this.openVipExperienceWin);
            }
            return;
        }// end function

        private function changeTab(event:DataEvent) : void
        {
            if (this.view.isHide)
            {
                this.view.show();
            }
            if (event.data == 1)
            {
                this._vipModule.addVipLevelInfoPanel();
            }
            else if (event.data == 2)
            {
                this._vipModule.addVipComparePanel();
            }
            return;
        }// end function

        private function updateDailyBtn(param1:Object) : void
        {
            this._vipModule.updateDailyBtn();
            return;
        }// end function

        private function newDayUpdate(param1:Object) : void
        {
            Cache.instance.vip.resetVipFreeTimes();
            if (this.isViewShow)
            {
                this._vipModule.updateDailyBtn();
            }
            return;
        }// end function

        public function openVipExperienceTimeOverWin() : void
        {
            VipExperienceWin.instance.show();
            VipExperienceWin.instance.updateState(true);
            return;
        }// end function

    }
}
